package com.wanmait.baohan.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.baohan.mapper.*;
import com.wanmait.baohan.pojo.Dictionary;
import com.wanmait.baohan.pojo.InitiateProject;
import com.wanmait.baohan.pojo.InitiateProjectMember;
import com.wanmait.baohan.pojo.Staff;
import com.wanmait.baohan.service.DictionaryService;
import com.wanmait.baohan.service.InitiateProjectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @author 打豆豆
 * @version 1.0
 * @date 2021/2/17 14:15
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InitiateProjectServiceImpl implements InitiateProjectService {

    @Resource
    private InitiateProjectMapper initiateProjectMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private WorkTemplateMapper workTemplateMapper;
    @Resource
    private InitiateProjectMemberMapper initiateProjectMemberMapper;
    @Resource
    private DictionaryMapper dictionaryMapper;

    @Override
    public void insert(InitiateProject object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public void update(InitiateProject object) {

    }

    @Override
    public InitiateProject findById(Integer id) {
        return null;
    }

    @Override
    public List<InitiateProject> findAll() {
        return null;
    }

    @Override
    public List<InitiateProject> findAll(InitiateProject object) {
        return null;
    }

    @Override
    public PageInfo<InitiateProject> findPage(int pageNum) {
        return null;
    }

    @Override
    public PageInfo<InitiateProject> findPage() {
        return null;
    }

    @Override
    public PageInfo<InitiateProject> findPage(int pageNum, InitiateProject object) {
        return null;
    }

    @Override
    public PageInfo<InitiateProject> findPage(InitiateProject object) {
        return null;
    }
    @Override
    public PageInfo<InitiateProject> findList(Integer page, Integer limit, Integer enable){
        PageHelper.startPage(page,limit);
        List<InitiateProject> initiateProjects = initiateProjectMapper.selectList(null);
        initiateProjects.forEach(initiateProject -> {
            initiateProject.setStaff(staffMapper.findOneById(initiateProject.getStaffId()));
            initiateProject.setWorkTemplate(workTemplateMapper.selectByPrimaryKey(initiateProject.getWorkTemplateId()));
        });
        PageInfo<InitiateProject> pageInfo = new PageInfo<>(initiateProjects, 7);
        return pageInfo;
    }

    @Override
    public void uploadInitiateProject(InitiateProject initiateProject) {
        initiateProjectMapper.insert(initiateProject);
        initiateProject.getMembers().forEach(staff -> {
            InitiateProjectMember initiateProjectMember = new InitiateProjectMember();
            initiateProjectMember.setInitiateProjectId(initiateProject.getId());
            initiateProjectMember.setStaffId(staff.getId());
            initiateProjectMemberMapper.insert(initiateProjectMember);
        });
    }

    @Override
    public List<InitiateProject> getAllInitiateProjectByWorkTemplateId(Integer id) {
        QueryWrapper<InitiateProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_template_id", id);
        List<InitiateProject> initiateProjects = initiateProjectMapper.selectList(queryWrapper);

        initiateProjects.forEach(initiateProject -> {

            initiateProject.setStaff(staffMapper.findOneById(initiateProject.getStaffId()));

            initiateProject.setWorkTemplate(workTemplateMapper.selectByPrimaryKey(initiateProject.getWorkTemplateId()));

            QueryWrapper<Dictionary> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.select("`value`").eq("dictionary_type_id", 36).eq("`key`",initiateProject.getProjectTypeId());
            initiateProject.setProjectTypeName(dictionaryMapper.selectOne(queryWrapper1).getValue());


            initiateProject.setMembers(staffMapper.selectInitiateProjectMember(initiateProject.getId()));
        });
        return initiateProjects;
    }
}
